<?php
namespace app\index\controller;
use think\Db;
use think\Session;

class Index
{
    public function index()
    {
        if(empty(session('openid'))){
            $code = isset($_GET['code'])?$_GET['code']:false;
            $appid = 'wxc54c32f29c629322';
            $appsecret = 'be73f1395f72be3ff6e7dfcf9ec20be0';
            $scope = 'snsapi_userinfo';
            $redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=$scope&state=STATE#wechat_redirect";
            if(!$code){
                header("Location:$url");
            }else{
                $url_code = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
                $code_obj = json_decode(file_get_contents($url_code));
                  if(!isset($code_obj->errcode)){
                    $access_token = $code_obj->access_token;
                    $openid = $code_obj->openid;
                    $url_userinfo = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
                    $userinfo_obj = json_decode(file_get_contents($url_userinfo));
                    if(!isset($userinfo_obj->errcode)){

                        session('openid',$userinfo_obj->openid);
                        session('nickname',$userinfo_obj->nickname);
                        session('sex',$userinfo_obj->sex);
                        session('province',$userinfo_obj->province);
                        session('city',$userinfo_obj->city);
                        session('country',$userinfo_obj->country);
                        session('headimgurl',$userinfo_obj->headimgurl);

                        $data['sid'] = session('thinksessionid');
                        $data['id'] = md5(session('openid'));
                        $data['nickname'] = session('nickname');
                        $data['sex'] = session('sex');
                        $data['province'] = session('province');
                        $data['city'] = session('city');
                        $data['country'] = session('country');
                        $data['headimgurl'] = session('headimgurl');
                        return view('index',$data);

                    }else{
                        //拉取用户信息失败
                        header("Location:$url");
                    }
                  }else{
                    //授权错误
                    header("Location:$url");
                  }
            }
        }else{
            $data['sid'] = session('thinksessionid');
            $data['id'] = md5(session('openid'));
            $data['nickname'] = session('nickname');
            $data['sex'] = session('sex');
            $data['province'] = session('province');
            $data['city'] = session('city');
            $data['country'] = session('country');
            $data['headimgurl'] = session('headimgurl');
            return view('index',$data);
        }
    }

    public function addEvent()
    {
        $data = ['name'=>'江西银行派对活动','description'=>'江西银行派对活动','status'=>'1'];
        Db::name('event')->insert($data);
    }

    public function verifyWeixinSSO()
    {
        if(!empty(session('openid'))){
            $data = [
                'resultCode'=>'0',
                'msg'=>'成功',
                'sid'=>session_id(),
                'id'=>md5(session('openid')),
                'nickname'=>session('nickname'),
                'sex'=>session('sex'),
                'province'=>session('province'),
                'city'=>session('city'),
                'country'=>session('country'),
                'headimgurl'=>session('headimgurl')
            ];
        }else{
            $data = [
                'resultCode'=>'1',
                'msg'=>'获取用户信息失败'
            ];
        }
        return json($data);
    }
}
